package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateDefendPatrons;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.StrongholdWarCrossProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Cmd(code = CrossProtocol.C_CROSS_STRONGHOLD_ELIMINATION_MEMBER_DEFEND_PATRONS,desc = "")
public class CrossStrongholdEliminateDefendPatronsListCmd extends CrossUnionCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        StrongholdWarCrossProto.StrongholdCrossDefendPatronsReqMsg req = StrongholdWarCrossProto.StrongholdCrossDefendPatronsReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        String unionId = req.getUnionId();
        StrongholdWarProto.UnionStrongholdDefendRespMsg.Builder resp = StrongholdWarProto.UnionStrongholdDefendRespMsg.newBuilder();
        BigInteger baseBlood = BigInteger.ZERO;
        List<CrossStrongholdEliminateDefendPatrons> defendPatrons = CrossStrongholdMgr.getEliminateDefendPatrons(activityId, unionId);
        Set<Long> battleMemberId = new HashSet<>();
        for (CrossStrongholdEliminateDefendPatrons patrons : defendPatrons) {
            StrongholdWarProto.UnionMemberDispatchPatronsTempMsg.Builder tempMsg = parseUnionMemberDispatchPatronsTempMsg(patrons);
            BigInteger earnSpeed = patrons.getPatrons().getTotalBlood();
            if (earnSpeed.compareTo(BigInteger.TEN) < 0) {
                earnSpeed = patrons.getEarnSpeed();
            }
            BigInteger blood = CrossStrongholdActivityMgr.calcBlood(activityId, earnSpeed);
            tempMsg.setBlood(blood.longValue());
            resp.addMemberData(tempMsg);
            baseBlood = baseBlood.add(blood);
            battleMemberId.add(patrons.getUserId());
        }
        resp.setStarLv(StrongholdWarMgr.getUnionBaseLv(activityId, unionId));
        resp.setBattleNum(battleMemberId.size());
        //千分比
        int hpAdd = StrongholdWarMgr.getHpAdd(activityId,unionId);
        BigInteger totalBlood = baseBlood.add(baseBlood.multiply(BigInteger.valueOf(hpAdd)).divide(BigInteger.valueOf(1000)));
        resp.setBaseBlood(baseBlood.longValue());
        resp.setTotalBlood(totalBlood.longValue());
        resp.setHpAdd(hpAdd);

        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_MEMBER_DEFEND_PATRONS, resp);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    public static  StrongholdWarProto.UnionMemberDispatchPatronsTempMsg.Builder parseUnionMemberDispatchPatronsTempMsg(CrossStrongholdEliminateDefendPatrons patrons) {
        StrongholdWarProto.UnionMemberDispatchPatronsTempMsg.Builder temp = StrongholdWarProto.UnionMemberDispatchPatronsTempMsg.newBuilder();
        temp.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(patrons.getUserBaseInfo()));
        temp.setPatrons(PatronsPb.parsePatronsTempMsg(patrons.getPatrons()));
        temp.setEarnSpeed(patrons.getPatrons().getAbility().longValue());
        temp.setBlood(patrons.getTotalBlood().longValue());
        temp.setUserId(patrons.getUserId());
        return temp;
    }

    @Override
    public void sendData() {

    }
}
